﻿using Common;
using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace NursingFormSystem.NursingRecordSheet
{
    public partial class SheetPrints : Form
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        DateTime Start;
        DateTime End;
        ElectronicSignature es;
        string pType;//打印本科室、非本科室
        DataTable IOData;
        DataTable BlankHeightData;
        float PageBlankHeight;
        bool continueflag;
        int continuenum;
        public SheetPrints(Tb_User loginUser, PatientInfo patientInfo, DateTime StartDate, DateTime EndDate, string printType, bool continueFlag, int continueNum)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            Start = StartDate;
            End = EndDate;
            es = new ElectronicSignature(this.LoginUser);
            pType = printType;
            continueflag = continueFlag;
            continuenum = continueNum;

            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\护理单(神经科).grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            if (continueflag)
            {
                GetBlankHeight();
                if ((BlankHeightData != null) && (BlankHeightData.Rows.Count > 0))
                {
                    float f = 29.7f - 3.4f - Convert.ToSingle(BlankHeightData.Rows[0]["BlackHeight"].ToString());
                    Report.ParameterByName("Parameter1").AsFloat = f;//此时应当是从获取对应的值
                }
            }
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            Report.PageProcessRecord += Report_PageProcessRecord;
            axGRPrintViewer1.Report = Report;
            Report.PrintEnd += Report_PrintEnd;
            Report.GeneratePagesEnd += Report_GeneratePagesEnd;
            GetSumIO();
        }
        //打印结束
        private void Report_PrintEnd()
        {
            //保存空白高度
            SetBlankHeight();
        }
        //打印结束
        private void Report_GeneratePagesEnd()
        {
            //得到空白高度
            PageBlankHeight = (float)Report.PageBlankHeight;
            //将参数赋值为空白页高度
        }
        private void GetSumIO()
        {
            string sql = @"SELECT SumDate,Group12,Group24,GroupDiy,BeginTime FROM [BNNursingDocsSystem_Develop].[dbo].[NursingFormSumIO](NOLOCK) where PatientId ='{0}' AND VisitId='{1}' AND FormId='{2}' ORDER BY SumDate ";
            sql = string.Format(sql, PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.NurseForm);
            IOData = new DataTable();
            IOData = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
        }
        private void GetBlankHeight()
        {
            string sql = @"SELECT BlackHeight FROM [BNNursingDocsSystem_Develop].[dbo].[NursingContinuePrint](NOLOCK) where PatientId ='{0}' AND VisitId='{1}'  AND FormId='{2}'";
            sql = string.Format(sql, PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.NurseForm);
            BlankHeightData = new DataTable();
            BlankHeightData = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
        }
        private void SetBlankHeight()
        {
            string sql = @"if(EXISTS (SELECT Id FROM NursingContinuePrint(NOLOCK) WHERE PatientId = '{0}' AND VisitId = '{1}' AND FormId='{3}'))
                            BEGIN UPDATE NursingContinuePrint SET  BlackHeight ='{4}' WHERE PatientId = '{0}' AND VisitId = '{1}' AND FormId='{3}' END ELSE BEGIN 
                            INSERT INTO NursingContinuePrint (PatientId,VisitId,PatientName,FormId,BlackHeight,PageNum) VALUES('{0}','{1}','{2}' ,'{3}','{4}',NULL) END";
            sql = string.Format(sql, PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, PatientInfo.PATIENT_NAME, (int)FormType.NurseForm, Convert.ToString(PageBlankHeight));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
        }
        private void ReportFetchRecord()
        {
            var ret = new ActionOperateResultOfString();
            if (pType == "本科室")
            {
                ret = formService.GetNursingFormRecordByAreaId(PatientInfo.PATIENT_ID.Trim(), PatientInfo.VISIT_ID, (int)FormType.NurseForm, Start, End, LoginUser.DepartmentCode);
            }
            else if (pType == "全部")
            {
                ret = formService.GetNursingFormRecordByDate(PatientInfo.PATIENT_ID.Trim(), PatientInfo.VISIT_ID, (int)FormType.NurseForm, Start, End);
            }
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();//先降序
                //如果有续打进行数据的处理；
                if ((list != null) && (continueflag))
                {
                    for (int m = list.Count - 1; m > 0; m--)
                    {
                        if (m > continuenum)
                        {
                            list.RemoveAt(m);
                        }
                    }
                }
                //再升序
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                string valueof24 = "1980-01-01";//判断当天是否包含24小时统计量的值
                string valueof24_1 = "1980-01-01";//判断当天是否包含24小时统计量的值
                string valueofdiy = "1980-01-01";//判断当天是否包含diy小时统计量的值
                string valueofdiy_1 = "1980-01-01";//判断当天是否包含diy小时统计量的值
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    ClinicalCare ps = XmlUtil.XMLToObject<ClinicalCare>(record1.NursingFormData, Encoding.UTF8);
                    if (ps == null) continue;

                    string newpageflag = "";
                    string group12 = "";
                    string group24 = "";
                    string groupdiy = "";
                    string riqisum = "";
                    string qianming;
                    string strShenzhi = "";
                    string strTongKongDuiGuangZuo = "";
                    string strTongKongDuiGuangYou = "";
                    string Vaule01 = "";
                    string Vaule02 = "";
                    string valueDiy01 = "";
                    string valueDiy02 = "";
                    string HoursDiy = "";
                    string TimeDiy = "00:00";
                    string image1 = "";
                    string RYHS = ps.QianMing;
                    string date = Convert.ToDateTime(ps.RiQi).ToString("yyyy-MM-dd");
                    string time = ps.ShiJian;

                    if (ps.IsNewPageFlag != null)
                    {
                        newpageflag = ps.IsNewPageFlag.ToString();
                    }
                    else
                    {
                        newpageflag = "";
                    }
                    riqisum = ps.RiQi;// 默认的初始值
                    //先给Group24的赋值
                    bool sumflag = true;//今日是否统计继续
                    if (valueof24_1.Contains(Convert.ToDateTime(date).ToString("yyyy-MM-dd")))
                    {
                        riqisum = Convert.ToDateTime(valueof24).AddDays(1).ToString("yyyy-MM-dd");
                        string _strtime1 = "07:00";
                        TimeSpan dsptime1 = DateTime.Parse(_strtime1).TimeOfDay;
                        TimeSpan dspnow = DateTime.Parse(time).TimeOfDay;
                        if (dspnow <= dsptime1)
                        {
                            group24 = valueof24 + " 00:00:00.024";
                            sumflag = false;
                        }
                    }
                    if (valueofdiy_1.Contains(Convert.ToDateTime(date).ToString("yyyy-MM-dd")))
                    {
                        object fullEndTime = null;//声明结束时间
                        riqisum = Convert.ToDateTime(valueofdiy).ToString("yyyy-MM-dd");//获取昨天的日期 即统计日期
                        DataRow row = IOData.Select(" sumdate = '" + riqisum + "'")[0];//获取统计日期对应的io表数据
                        object obj_stime = row["BeginTime"];//取得数据起始时间
                        object obj_groupdiy = row["GroupDiy"];//取得数据统计小时数
                        if (obj_stime == DBNull.Value) obj_stime = "07:00";
                        if (obj_groupdiy == DBNull.Value) obj_groupdiy = "0";
                        HoursDiy = obj_groupdiy.ToString();//grf字段赋值
                        //算出结束时间
                        fullEndTime = Convert.ToDateTime(riqisum + " " + obj_stime).AddHours(Convert.ToInt32(obj_groupdiy));
                        TimeDiy = Convert.ToDateTime(fullEndTime).AddMinutes(1).ToString("HH:mm");//结束时间字段赋值
                        //如果结束日期和起始日期不一致 判定为第二天 显示为第二天的日期
                        if (!Convert.ToDateTime(fullEndTime).ToString("yyyy-MM-dd").Equals(riqisum))
                            riqisum = Convert.ToDateTime(riqisum).AddDays(1).ToString("yyyy-MM-dd");
                        //算出完整起始时间
                        DateTime fullBeginTime = Convert.ToDateTime(valueofdiy + " " + obj_stime);
                        //算出完整护理时间
                        DateTime fullItemTime = Convert.ToDateTime(date + " " + time);
                        //完整护理时间介于起始时间和结束时间则显示统计标识组
                        if (fullItemTime >= fullBeginTime && fullItemTime <= Convert.ToDateTime(fullEndTime))
                        {
                            groupdiy = Convert.ToDateTime(fullEndTime).ToString("yyyy-MM-dd 00:00:00.001");
                            sumflag = false;
                        }
                    }
                    if (sumflag)
                    {
                        //根据统计的标识来加载赋值是否显示统计行
                        if (IOData.Rows.Count > 0)
                        {
                            for (int j = 0; j < IOData.Rows.Count; j++)
                            {
                                bool endflag = false;//统计标识
                                string tempdate = Convert.ToDateTime(IOData.Rows[j]["SumDate"]).ToString("yyyy-MM-dd");
                                if (tempdate.Contains(date))//相同的日期
                                {
                                    //12小时的 Y判断
                                    if ((IOData.Rows[j]["Group12"] + "").Contains("Y"))
                                    {
                                        riqisum = date;
                                        string _strtime1 = "07:01";
                                        string _strtime2 = "19:00";
                                        TimeSpan dsptime1 = DateTime.Parse(_strtime1).TimeOfDay;
                                        TimeSpan dsptime2 = DateTime.Parse(_strtime2).TimeOfDay;
                                        TimeSpan dspnow = DateTime.Parse(time).TimeOfDay;
                                        if ((dspnow >= dsptime1) && (dspnow <= dsptime2))
                                        {
                                            group12 = Convert.ToDateTime(IOData.Rows[j]["SumDate"]).ToString("yyyy-MM-dd HH:mm:ss") + ".012";
                                            endflag = true;
                                        }
                                    }

                                    //24小时的 Y判断
                                    if ((IOData.Rows[j]["Group24"] + "").Contains("Y"))
                                    {
                                        riqisum = Convert.ToDateTime(date).AddDays(1).ToString("yyyy-MM-dd");
                                        string _strtime1 = "07:01";
                                        TimeSpan dsptime1 = DateTime.Parse(_strtime1).TimeOfDay;//起始时间
                                        TimeSpan dspnow = DateTime.Parse(time).TimeOfDay;//护理时间
                                        if (dspnow >= dsptime1)
                                        {
                                            group24 = Convert.ToDateTime(IOData.Rows[j]["SumDate"]).ToString("yyyy-MM-dd HH:mm:ss") + ".024";
                                            endflag = true;
                                        }
                                        //获取下一天的日期，并且赋值给参数，来判断24小时第二天的统计值
                                        DateTime tempdt01 = Convert.ToDateTime(date);
                                        valueof24 = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
                                        valueof24_1 = tempdt01.AddDays(1).ToString("yyyy-MM-dd");
                                    }

                                    //自定义小时
                                    //报表 HoursDiy 字段赋值
                                    string tempHoursDiy = (IOData.Rows[j]["GroupDiy"] != DBNull.Value && IOData.Rows[j]["GroupDiy"] != null) ? (IOData.Rows[j]["GroupDiy"]).ToString() : "";
                                    if (!string.IsNullOrEmpty(tempHoursDiy) && Regex.IsMatch(tempHoursDiy, @"^\d+$") && Convert.ToInt32(tempHoursDiy) > 0)
                                    {
                                        //统计准备字段
                                        string tempDate = Convert.ToDateTime(IOData.Rows[j]["SumDate"]).ToString("yyyy-MM-dd");//统计日期
                                        string stime = (IOData.Rows[j]["BeginTime"] != DBNull.Value && IOData.Rows[j]["BeginTime"] != null) ? (IOData.Rows[j]["BeginTime"]).ToString() : "07:01";//获得开始时间
                                        DateTime etime = Convert.ToDateTime(tempDate + " " + stime).AddHours(Convert.ToInt32(tempHoursDiy));//算出结束时间
                                        //报表 TimeDiy 字段赋值
                                        TimeDiy = etime.AddMinutes(1).ToString("HH:mm");//自定义开始时间
                                        riqisum = tempDate;
                                        bool nextDay = false;
                                        if (!etime.ToString("yyyy-MM-dd").Equals(tempDate))
                                        {
                                            riqisum = Convert.ToDateTime(riqisum).AddDays(1).ToString("yyyy-MM-dd");
                                            nextDay = true;
                                        }
                                        string _strtime1 = stime;
                                        string _strtime2 = TimeDiy;
                                        TimeSpan dsptime1 = DateTime.Parse(_strtime1).TimeOfDay;//起始时间
                                        TimeSpan dspnow = DateTime.Parse(time).TimeOfDay;//护理时间

                                        DateTime fullstime = (Convert.ToDateTime(tempDate).AddHours(dsptime1.Hours)).AddMinutes(dsptime1.Minutes);//完整起始时间
                                        DateTime itemTime = Convert.ToDateTime(date).AddHours(dspnow.Hours).AddMinutes(dspnow.Minutes);//完整护理时间

                                        if ((itemTime >= fullstime) && itemTime <= etime)
                                        {
                                            groupdiy = Convert.ToDateTime(IOData.Rows[j]["SumDate"]).ToString("yyyy-MM-dd 00:00:00.001");
                                            HoursDiy = tempHoursDiy;
                                            endflag = true;
                                        }
                                        if (Convert.ToDateTime(tempDate).AddDays(1).ToString("yyyy-MM-dd") == etime.ToString("yyyy-MM-dd"))
                                        {
                                            //获取下一天的日期，并且赋值给参数，来判断diy小时第二天的统计值
                                            DateTime tempdt01 = Convert.ToDateTime(date);
                                            valueofdiy = date.ToString().Replace("-", "/");
                                            valueofdiy_1 = tempdt01.AddDays(1).ToString("yyyy-MM-dd");
                                        }
                                    }

                                    if (endflag)
                                    {
                                        break;
                                    }

                                }
                            }
                        }
                        else
                        {
                            group12 = "";
                            group24 = "";
                            groupdiy = "";
                        }
                    }
                    if (!string.IsNullOrEmpty(RYHS))
                    {
                        image1 = es.GetElectricSignBase64(record1.AreaId, ps.QianMing);
                    }
                    if (!Base.ESSwitch.Equals("on"))
                    {
                        qianming = ps.QianMing;
                    }
                    else
                    {
                        qianming = image1;
                    }
                    if (!string.IsNullOrEmpty(ps.ShenZhi))
                        strShenzhi = ps.ShenZhi.Trim();
                    if (!string.IsNullOrEmpty(ps.TongKongDuiGuangZuo))
                        strTongKongDuiGuangZuo = ps.TongKongDuiGuangZuo.Trim();
                    if (!string.IsNullOrEmpty(ps.TongKongDuiGuangYou))
                        strTongKongDuiGuangYou = ps.TongKongDuiGuangYou.Trim();
                    switch (strShenzhi)
                    {
                        //注：神志：清醒(√) 嗜睡(-) 朦胧(±) 浅昏迷(+) 中昏迷(++) 深昏迷(+++) 麻醉未醒(△)
                        //    瞳孔对光反应：灵敏(++) 迟钝(+) 消失(-)
                        case "清醒":
                            strShenzhi = "√";
                            break;
                        case "清楚":
                            strShenzhi = "√";
                            break;
                        case "嗜睡":
                            strShenzhi = "-";
                            break;
                        case "朦胧":
                            strShenzhi = "±";
                            break;
                        case "浅昏迷":
                            strShenzhi = "+";
                            break;
                        case "中昏迷":
                            strShenzhi = "++";
                            break;
                        case "深昏迷":
                            strShenzhi = "+++";
                            break;
                        case "麻醉未醒":
                            strShenzhi = "△";
                            break;
                        default:
                            break;
                    }
                    switch (strTongKongDuiGuangZuo)
                    {
                        case "灵敏":
                            strTongKongDuiGuangZuo = "++";
                            break;
                        case "迟钝":
                            strTongKongDuiGuangZuo = "+";
                            break;
                        case "消失":
                            strTongKongDuiGuangZuo = "-";
                            break;
                    }
                    switch (strTongKongDuiGuangYou)
                    {
                        case "灵敏":
                            strTongKongDuiGuangYou = "++";
                            break;
                        case "迟钝":
                            strTongKongDuiGuangYou = "+";
                            break;
                        case "消失":
                            strTongKongDuiGuangYou = "-";
                            break;
                    }

                    //计算字符数(输入输出文字对齐还未完成)
                    int lenth_ShuRuYeTiMingCheng = ps.ShuRuYeTiMingCheng.Length;
                    int lenth_ShuRuYeTiLiang = ps.ShuRuYeTiLiang.Length;
                    int lenth_ShuRuYeTiTuJing = ps.ShuRuYeTiTuJing.Length;
                    //统计12 24小时的
                    float tempinput = 0;
                    if (ps.ShuRuYeTiLiang.Contains(","))
                    {
                        string[] str = ps.ShuRuYeTiLiang.Split(',');
                        for (int j = 0; j < str.Length; j++)
                        {
                            if (RegexHelper.IsFloat(str[j]))
                            {
                                tempinput = tempinput + Convert.ToSingle(str[j]);
                            }
                        }
                        Vaule01 = Convert.ToString(tempinput);
                    }
                    else
                    {
                        Vaule01 = ps.ShuRuYeTiLiang;
                    }
                    float tempout = 0;
                    if (ps.PaiChuYeLiangLiang.Contains(","))
                    {
                        string[] str = ps.PaiChuYeLiangLiang.Split(',');
                        for (int j = 0; j < str.Length; j++)
                        {
                            if (RegexHelper.IsFloat(str[j]))
                            {
                                tempout = tempout + Convert.ToSingle(str[j]);
                            }
                        }
                        Vaule02 = Convert.ToString(tempout);
                    }
                    else
                    {
                        Vaule02 = ps.PaiChuYeLiangLiang;
                    }

                    //统计自定义的
                    if (!string.IsNullOrEmpty(groupdiy))
                    {
                        float tempinputd = 0;
                        if (ps.ShuRuYeTiLiang.Contains(","))
                        {
                            string[] str = ps.ShuRuYeTiLiang.Split(',');
                            for (int j = 0; j < str.Length; j++)
                            {
                                if (RegexHelper.IsFloat(str[j]))
                                {
                                    tempinputd = tempinputd + Convert.ToSingle(str[j]);
                                }
                            }
                            valueDiy01 = Convert.ToString(tempinputd);
                        }
                        else
                        {
                            valueDiy01 = ps.ShuRuYeTiLiang;
                        }

                        float tempoutd = 0;
                        if (ps.PaiChuYeLiangLiang.Contains(","))
                        {
                            string[] str = ps.PaiChuYeLiangLiang.Split(',');
                            for (int j = 0; j < str.Length; j++)
                            {
                                if (RegexHelper.IsFloat(str[j]))
                                {
                                    tempoutd = tempoutd + Convert.ToSingle(str[j]);
                                }
                            }
                            valueDiy02 = Convert.ToString(tempoutd);
                        }
                        else
                        {
                            valueDiy02 = ps.PaiChuYeLiangLiang;
                        }
                    }

                    //当传入字符串为空值时，会影响Report.FieldByName("IsNewPageFlag")值获取的问题，具体原因未知，先给起赋值一个初值，不影响显示和判断
                    if (image1 == "")
                    {
                        image1 = "image";
                    }

                    #region 对入量的多个输入进行显示排序
                    if (ps.ShuRuYeTiMingCheng.Contains(",")) //有多个输入量名称
                    {
                        string[] tempintakename = ps.ShuRuYeTiMingCheng.Split(',');
                        string[] tempintakevalue = ps.ShuRuYeTiLiang.Split(',');
                        string[] tempintakediway = ps.ShuRuYeTiTuJing.Split(',');

                        ps.ShuRuYeTiMingCheng = ""; ps.ShuRuYeTiLiang = ""; ps.ShuRuYeTiTuJing = "";
                        for (int j = 0; j < tempintakename.Length; j++)
                        {
                            if (!string.IsNullOrEmpty(tempintakename[j]))
                            {
                                //报表中入量名称一行有12个字节
                                int rows1 = 0;//初始行数
                                int templength1 = Encoding.Default.GetByteCount(tempintakename[j]);
                                int Remainder1 = templength1 % 13;
                                if ((Remainder1 == 0) || (templength1 < 13))
                                {
                                    rows1 = templength1 / 13;
                                }
                                else
                                {
                                    rows1 = (templength1 / 13) + 1;
                                }
                                //报表中入量值一行有5个字节
                                int rows2 = 0;//初始行数
                                int templength2 = Encoding.Default.GetByteCount(tempintakevalue[j]);
                                int Remainder2 = templength2 % 5;
                                if ((Remainder2 == 0) || (templength2 < 5))
                                {
                                    rows2 = templength2 / 5;
                                }
                                else
                                {
                                    rows2 = (templength2 / 5) + 1;
                                }
                                //报表中入量途径一行有6个字节
                                int rows3 = 0;//初始行数
                                int templength3 = Encoding.Default.GetByteCount(tempintakediway[j]);
                                int Remainder3 = templength3 % 9;
                                if ((Remainder3 == 0) || (templength3 < 9))
                                {
                                    rows3 = templength3 / 9;
                                }
                                else
                                {
                                    rows3 = (templength3 / 9) + 1;
                                }

                                if (rows1 > rows2)
                                {
                                    for (int m = 0; m < (rows1 - rows2); m++)
                                    {
                                        tempintakevalue[j] = tempintakevalue[j] + "\r\n";
                                    }
                                }

                                if (rows1 > rows3)
                                {
                                    for (int m = 0; m < (rows1 - rows3); m++)
                                    {
                                        tempintakediway[j] = tempintakediway[j] + "\r\n";
                                    }
                                }

                                ps.ShuRuYeTiMingCheng = ps.ShuRuYeTiMingCheng + tempintakename[j] + "\r\n\r\n";
                                if (rows2 > rows1)
                                {
                                    ps.ShuRuYeTiLiang = ps.ShuRuYeTiLiang + tempintakevalue[j] + "\r\n";
                                }
                                else
                                {
                                    ps.ShuRuYeTiLiang = ps.ShuRuYeTiLiang + tempintakevalue[j] + "\r\n\r\n";
                                }
                                if (rows3 > rows1)
                                {
                                    ps.ShuRuYeTiTuJing = ps.ShuRuYeTiTuJing + tempintakediway[j] + "\r\n";
                                }
                                else
                                {
                                    ps.ShuRuYeTiTuJing = ps.ShuRuYeTiTuJing + tempintakediway[j] + "\r\n\r\n";
                                }
                            }
                        }
                    }
                    #endregion

                    #region 对出量的多个输入进行显示排序
                    if (ps.PaiChuYeLiangMingCheng.Contains(",")) //有多个输入量名称
                    {
                        string[] tempdischargename = ps.PaiChuYeLiangMingCheng.Split(',');
                        string[] tempdischargevalue = ps.PaiChuYeLiangLiang.Split(',');

                        ps.PaiChuYeLiangMingCheng = ""; ps.PaiChuYeLiangLiang = "";
                        for (int j = 0; j < tempdischargename.Length; j++)
                        {
                            if (!string.IsNullOrEmpty(tempdischargename[j]))
                            {
                                //报表中出量名称一行有8个字节
                                int rows1 = 0;//初始行数
                                int templength1 = Encoding.Default.GetByteCount(tempdischargename[j]);
                                int Remainder1 = templength1 % 8;
                                if (Remainder1 == 0)
                                {
                                    rows1 = templength1 / 8;
                                }
                                else
                                {
                                    rows1 = (templength1 / 8) + 1;
                                }
                                //报表中出量值一行有4个字节
                                int rows2 = 0;//初始行数
                                int templength2 = Encoding.Default.GetByteCount(tempdischargevalue[j]);
                                int Remainder2 = templength2 % 4;
                                if (Remainder2 == 0)
                                {
                                    rows2 = templength2 / 4;
                                }
                                else
                                {
                                    rows2 = (templength2 / 4) + 1;
                                }

                                if (rows1 >= rows2)
                                {
                                    for (int m = 0; m < (rows1 - rows2); m++)
                                    {
                                        tempdischargevalue[j] = tempdischargevalue[j] + "\r\n";
                                    }
                                }

                                ps.PaiChuYeLiangMingCheng = ps.PaiChuYeLiangMingCheng + tempdischargename[j] + "\r\n\r\n";
                                if (rows2 > rows1)
                                {
                                    ps.PaiChuYeLiangLiang = ps.PaiChuYeLiangLiang + tempdischargevalue[j] + "\r\n";
                                }
                                else
                                {
                                    ps.PaiChuYeLiangLiang = ps.PaiChuYeLiangLiang + tempdischargevalue[j] + "\r\n\r\n";
                                }
                            }
                        }
                    }
                    #endregion

                    #region DataTable添加数据
                    DataRow newRow = dt.NewRow();
                    newRow["DEPARTMENT_NAME"] = PatientInfo.DEPARTMENT_NAME;//DEPARTMENT_NAME
                    newRow["NURSE_CELL_NAME"] = PatientInfo.NURSE_CELL_NAME;//NURSE_CELL_NAME
                    newRow["BED_NO"] = PatientInfo.BED_NO;//BED_NO
                    newRow["PatientName"] = PatientInfo.PATIENT_NAME;//姓名
                    newRow["SEX"] = PatientInfo.SEX;//SEX
                    newRow["AGE"] = PatientInfo.AGE;//AGE
                    newRow["PatientId"] = PatientInfo.PATIENT_ID;//PATIENTID
                    newRow["IN_DATE"] = PatientInfo.IN_DATE;//入院日期
                    newRow["IN_DEPARTMENT_DATE"] = PatientInfo.IN_DEPARTMENT_DATE;//入科日期
                    newRow["Time"] = DateTimeHelper.HHmmssToHHmm(ps.ShiJian);
                    newRow["Temper"] = ps.TiWen;
                    newRow["Pulse"] = ps.MaiBo;
                    newRow["HeartRate"] = ps.XinLv;
                    newRow["Breath"] = ps.HuXi;
                    newRow["xueya"] = ps.XueYa;
                    newRow["ppp"] = date;
                    newRow["Spo2"] = ps.Spo;
                    newRow["StandUp"] = ps.FanShen == "1" ? "√" : "";
                    newRow["shenzhi"] = strShenzhi;
                    newRow["tongkongdaxiaozuo"] = ps.TongKongDaXiaoZuo;
                    newRow["tongkongdaxiaoyou"] = ps.TongKongDaXiaoYou;
                    newRow["tongkongduiguangzuo"] = strTongKongDuiGuangZuo;
                    newRow["tongkongduiguangyou"] = strTongKongDuiGuangYou;
                    newRow["Intakename"] = ps.ShuRuYeTiMingCheng;//输入液体
                    newRow["Intakevalue"] = ps.ShuRuYeTiLiang;
                    newRow["Intakediway"] = ps.ShuRuYeTiTuJing;
                    newRow["Dischargename"] = ps.PaiChuYeLiangMingCheng;//排出液体
                    newRow["Dischargevalue"] = ps.PaiChuYeLiangLiang;
                    newRow["bingqingzhiliaojihulicuoshi"] = ps.BingQingZhiLiaoJiHuLiCuoShi;
                    newRow["RiQi"] = date;//日期
                    newRow["NursName"] = qianming;//签名
                    newRow["ElectricSignBase64"] = image1;//电签
                    newRow["IsNewPageFlag"] = newpageflag;//另起一页
                    newRow["Group12"] = group12;//另起一页
                    newRow["Group24"] = group24;//另起一页
                    newRow["Vaule01"] = Vaule01;//合计入量
                    newRow["Vaule02"] = Vaule02;//合计出量
                    newRow["RiQiSum"] = riqisum;//统计行显示日期
                    newRow["TimeDiy"] = TimeDiy;//
                    newRow["HoursDiy"] = HoursDiy;//
                    newRow["HoursDiyOut"] = HoursDiy;//
                    newRow["Groupdiy"] = groupdiy;//
                    newRow["ValueDiy01"] = valueDiy01;
                    newRow["ValueDiy02"] = valueDiy02;
                    dt.Rows.Add(newRow);
                    #endregion
                }


                dt.DefaultView.Sort = "RiQi ASC,Time ASC";
                DataTable data = dt.DefaultView.ToTable();
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        //根据加载的值来判断是否另起一页
        private void Report_PageProcessRecord()
        {
            IGRField ob = Report.FieldByName("IsNewPageFlag");
            if ((ob != null) && (ob.Value != null))
            {
                if (ob.Value.ToString().Contains("Y"))
                {
                    Report.ForceNewPage();//强制另起一页
                }
            }
        }
        /// <summary>
        /// 加载列头
        /// </summary>
        /// <param name="dt"></param>
        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
            dt.Columns.Add("IN_DATE", typeof(string));
            dt.Columns.Add("IN_DEPARTMENT_DATE", typeof(string));
            dt.Columns.Add("Time", typeof(string));
            dt.Columns.Add("Temper", typeof(string));
            dt.Columns.Add("Pulse", typeof(string));
            dt.Columns.Add("HeartRate", typeof(string));
            dt.Columns.Add("Breath", typeof(string));
            dt.Columns.Add("xueya", typeof(string));
            dt.Columns.Add("ppp", typeof(string));
            dt.Columns.Add("Spo2", typeof(string));
            dt.Columns.Add("StandUp", typeof(string));
            dt.Columns.Add("shenzhi", typeof(string));
            dt.Columns.Add("tongkongdaxiaozuo", typeof(string));
            dt.Columns.Add("tongkongdaxiaoyou", typeof(string));
            dt.Columns.Add("tongkongduiguangzuo", typeof(string));
            dt.Columns.Add("tongkongduiguangyou", typeof(string));
            dt.Columns.Add("Intakename", typeof(string));
            dt.Columns.Add("Intakevalue", typeof(string));
            dt.Columns.Add("Intakediway", typeof(string));
            dt.Columns.Add("Dischargename", typeof(string));
            dt.Columns.Add("Dischargevalue", typeof(string));
            dt.Columns.Add("bingqingzhiliaojihulicuoshi", typeof(string));
            dt.Columns.Add("RiQi", typeof(string));//日期
            dt.Columns.Add("NursName", typeof(string));//签名
            dt.Columns.Add("ElectricSignBase64", typeof(string));//电签
            dt.Columns.Add("IsNewPageFlag", typeof(string));//另起一页
            dt.Columns.Add("Group12", typeof(string));//另起一页
            dt.Columns.Add("Group24", typeof(string));//另起一页
            dt.Columns.Add("Vaule01", typeof(string));//合计入量
            dt.Columns.Add("Vaule02", typeof(string));//合计出量
            dt.Columns.Add("RiQiSum", typeof(string));//统计行显示日期
            dt.Columns.Add("TimeDiy", typeof(string));//
            dt.Columns.Add("HoursDiy", typeof(string));//
            dt.Columns.Add("HoursDiyOut", typeof(string));//
            dt.Columns.Add("Groupdiy", typeof(string));//
            dt.Columns.Add("ValueDiy01", typeof(string));
            dt.Columns.Add("ValueDiy02", typeof(string));
        }
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        private void SheetPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
